<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>添加新地址</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <link rel="stylesheet" type="text/css" href="../css/swiper.min.css" />
    <link rel="stylesheet" type="text/css" href="../css/layer.css" />
    <link rel="stylesheet" type="text/css" href="../css/mdw.css" />
    <script type="text/javascript" src="../script/api.js"></script>
    <script type="text/javascript" src="../script/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../script/swiper.min.js"></script>
    <script type="text/javascript" src="../script/mobile.js"></script>
    <script type="text/javascript" src="../script/layer.js"></script>
    <script type="text/javascript" src="../script/mdw.js"></script>
    <script type="text/javascript" src="../script/jumpPage.js"></script>

</head>
<body class="bg-eee">
    <header class="header">
        <div class="backPrevPage on" onclick="historyBack()"></div>
        <div class="pageTitle">添加新地址</div>

    </header>
    <section class="addPage HTML_BOX">
        <div class="bg-eee" style="border-bottom:.2rem solid #EEE ; "></div>
        <ul class="addBox settings plr bg-fff">
            <li class="">
                <div class="boxTit"><span class="tursIcon"></span>姓</div>
                <div class="inputInfo"><input id="name" placeholder="姓" tyoe="text" /></div>
            </li>
            <li class="">
                <div class="boxTit"><span class="tursIcon"></span>名</div>
                <div class="inputInfo"><input id="name" placeholder="名" tyoe="text" /></div>
            </li>
            <li class="">
                <div class="boxTit"><span class="tursIcon"></span>联系电话</div>
                <div class="inputInfo"><input id="phone" placeholder="请输入收货人电话" tyoe="text" /></div>
            </li>
           <li class="">
                <div class="boxTit"><span class="tursIcon"></span>邮箱</div>
                <div class="inputInfo"><input id="eamil" placeholder="请输入收货人邮箱" tyoe="text" /></div>
            </li>
             <li class="">
                <div class="boxTit"><span class="tursIcon"></span>国家</div>
                <div id="cgj" class="inputInfo selectBox"><span>请选择国家</span><i></i><input id="guojia" value="0" type="hidden" /></div>
            </li>
            <li class="">
                <div class="boxTit"><span class="tursIcon"></span>省份</div>
                 <div id="csf" class="inputInfo selectBox"><span>请选择省份</span><i></i><input id="sf" value="0" type="hidden" /></div>
            </li>
            <li class="">
                <div class="boxTit"><span class="tursIcon"></span>城市</div>
                <div id="ccs" class="inputInfo selectBox"><span>请选择城市</span><i></i><input id="chengshi" value="0" type="hidden" /></div>
            </li>
            <li class="dn">
                <div class="boxTit"><span class="tursIcon"></span>城市</div>
                <div class="inputInfo"><input placeholder="请输入所在城市" tyoe="text" /></div>
            </li>
            <li class="">
                <div class="boxTit"><span class="tursIcon"></span>街道</div>
                <div class="inputInfo"><input placeholder="请填写街道及门牌号" tyoe="text" /></div>
            </li>
            <li class="">
                <div class="boxTit"><span class=""></span>公寓</div>
                <div class="inputInfo"><input placeholder="请填写单元号" tyoe="text" /></div>
            </li>
            <li class="">
                <div class="boxTit"><span class="tursIcon"></span>邮编</div>
                <div class="inputInfo"><input placeholder="请填写邮箱" tyoe="text" /></div>
            </li>
            <li class="">
                <div class="boxTit"><span class=""></span>Buzz Code</div>
                <div class="inputInfo"><input placeholder="请填写Buzz Code" tyoe="text" /></div>
            </li>
        </ul>
        <div class="loginBtn f_c">保存</div>
        <div class="bg-eee" style="border-bottom:.5rem solid #EEE ; "></div>
    </section>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
    var param = '';
    apiready = function(){
        $api.fixStatusBar($api.dom('header'));
        api.setStatusBarStyle({
            color: '#fff',//设置APP状态栏背景色
            style: 'dark'
        });
        //选择国家后回调
        api.addEventListener({
            name: 'chooseloading'
        }, function(ret, err) {
            var data =ret.value;
            $("#guojia").val(data.id);
            $("#cgj span").html(data.name);
            $("#cgj span").addClass("on");
            $("#sf").val('0');
            $("#chengshi").val('0');
            $("#csf span").removeClass("on");
            $("#csf span").html('请选择省份');
            if(cnen==2){
                $("#csf span").html('Please choose your province');
            }
            $("#ccs span").removeClass("on");
            $("#ccs span").html('请选择城市');
            $(".addBox li").eq(7).addClass("dn");
            $(".addBox input").eq(7).val("");
            if(cnen==2){
                $("#ccs span").html('Please choose your city');
            }
        });
        //选择省份
        api.addEventListener({
            name: 'province'
        }, function(ret, err) {
            var data =ret.value;
            $("#sf").val(data.id);
            $("#csf span").html(data.name);
            $("#csf span").addClass("on");

            $("#chengshi").val('0');
            $("#ccs span").removeClass("on");
            $("#ccs span").html('请选择城市');
            $(".addBox li").eq(7).addClass("dn");
            $(".addBox input").eq(7).val("");
            if(cnen==2){
                $("#ccs span").html('Please choose your city');
            }
          
        });
        //选择城市
        api.addEventListener({
            name: 'choosecity'
        }, function(ret, err) {
            var data =ret.value;
            $("#chengshi").val(data.id);
            $("#ccs span").html(data.name);
            $("#ccs span").addClass("on");
            if(data.name=='All Cities'){
                $(".addBox li").eq(7).removeClass("dn");
            }

        });
        ifCnAndEn();
        param = api.pageParam;
        if(param.id){
            ready();
        }

    };
    //选择国家
    $("#cgj").click(function(){
        openWin('address_choose','address_choose.html');
    })
    //选择省份
    $("#csf").click(function(){
        var id = $("#guojia").val();
        if(id==0){
            if(cnen ==1){
                pop.notice("请选择国家");
            }else{
                pop.notice('Please choose the country');
            }
            return;
        }
        openWin('address_choose_province','address_choose_province.html',{country_id:id});
    })
    //选择城市
    $("#ccs").click(function(){
        var id = $("#sf").val();
        if(id==0){
            if(cnen ==1){
                pop.notice("请选择省份");
            }else{
                pop.notice('Please choose the province');
            }
            return;
        }
        openWin('address_choose_city','address_choose_city.html',{province_id:id,type:true});
    })

    
    //提交
    $(".loginBtn").click(function(){
        var layers = "";
        var isnull = false;
        $(".addBox input").each(function(i,item){
            var val = $(item).val();
            if(val == "" && !isnull){
                switch(i){
                    case 0:
                    layers = "请输入收货人姓";
                    if(cnen==2){
                        layers = "Please enter your last name.";
                    }
                    isnull = true;
                    break;
                    case 1:
                    layers = "请输入收货人名";
                    if(cnen==2){
                        layers = "Please enter your name.";
                    }
                    case 2:
                    layers = "请输入收货人电话";
                    if(cnen==2){
                        layers = "Please enter your name.";
                    }
                    isnull = true;
                    break;
                    case 3:
                    layers = "请输入收货人邮箱";
                    if(cnen==2){
                        layers = "Please enter the consignee's e-mail";
                    }
                    isnull = true;
                    break;
                    case 4:
                    layers = "请选择国家";
                    if(cnen==2){
                        layers = "Please choose your country";
                    }
                    isnull = true;
                    break;
                    case 5:
                    layers = "请选择省份";
                    if(cnen==2){
                        layers = "Please choose your province";
                    }
                    isnull = true;
                    break;
                    case 6:
                    layers = "请选择城市";
                    if(cnen==2){
                        layers = "Please choose your city";
                    }
                    isnull = true;
                    break;
                    case 8:
                    layers = "请填写街道及门牌号";
                    if(cnen==2){
                        layers = "Please enter the address";
                    }
                    isnull = true;
                    break;
                    case 10:
                    layers = "请填写邮政编码";
                    if(cnen==2){
                        layers = "Please enter the post code";
                    }
                    isnull = true;
                    break;
                }
            }
           
        });
        var other_cities="";
        if($("#ccs span").html() == 'All Cities' && !isnull){
            other_cities=$(".addBox input").eq(7).val();
            if(other_cities==""){
                isnull = true;
                layers = "请输入城市";
                if(cnen==2){
                    layers= "Please enter the your city";
                }
            }
        }
        if(isnull){
            console.log(layers);
            pop.notice(layers);
            return;
        }
        

        var type = 1;
        if(param.type == 1){
            type =2;
        }
        var p = {
            token:token,
            type:type,
            username:$(".addBox input").eq(1).val(),
            name:$(".addBox input").eq(0).val(),
            email:$(".addBox input").eq(3).val(),
            mobile:$(".addBox input").eq(2).val(),
            country_id:$(".addBox input").eq(4).val(),
            province_id:$(".addBox input").eq(5).val(),
            city_id:$(".addBox input").eq(6).val(),
            you_code:$(".addBox input").eq(10).val(),
            address:$(".addBox input").eq(8).val(),
            gongyu:$(".addBox input").eq(9).val(),
            buzz_code:$(".addBox input").eq(11).val(),
            other_cities:other_cities,
            id:param.id,
        };
        pop.loading();
        getAjax(Interface.add_address,p,function(res){
            pop.close();
            api.sendEvent({
                name: 'add_address',
                extra: {}
            });
            if(type==2){
                api.sendEvent({
                    name: 'order_address',
                    extra: {}
                });
            }
            pop.notice(res.msg,1.5,function(){
                historyBack();
            });
        },function(res,err){
            
        })
    });


    function ready(){
        if(cnen==1){
            $(".pageTitle").html('修改地址');
        }else{
            $(".pageTitle").html('Update address');
        }
        var p = {token:token,address_id:param.id};
        getAjax(Interface.address_id,p,function(res){
            console.log(JSON.stringify(res));
            var data =res.data;
            data.name=data.name||"";
            $(".addBox input").eq(0).val(data.name);
            $(".addBox input").eq(1).val(data.username);
            $(".addBox input").eq(2).val(data.mobile);
            $(".addBox input").eq(3).val(data.email);
            $(".addBox input").eq(4).val(data.country_id);
            $(".addBox input").eq(5).val(data.province_id);
            $(".addBox input").eq(6).val(data.city_id);
            $(".addBox input").eq(7).val(data.other_cities);
            $(".addBox input").eq(8).val(data.address);
            $(".addBox input").eq(9).val(data.gongyu);
            $(".addBox input").eq(10).val(data.you_code);
            $(".addBox input").eq(11).val(data.buzz_code || "");
            $("#csf span").html(data.province_name).addClass("on");
            $("#cgj span").html(data.country_name).addClass("on");
            $("#ccs span").html(data.city_name).addClass("on");
            if(data.city_name =='All Cities'){
                $(".addBox li").eq(7).removeClass("dn");
            }
        },function(res,err){})
    }
</script>
<script type="text/javascript">
$(function(){
  ifCnAndEn();
})
    function ifCnAndEn(){
        if(cnen==2){
            $(".pageTitle").html("Add new address");
            $(".settings li").eq(0).find(".boxTit").html('<span class="tursIcon"></span>Last Name');
            $(".settings li").eq(1).find(".boxTit").html('<span class="tursIcon"></span>First Name');
            $(".settings li").eq(2).find(".boxTit").html('<span class="tursIcon"></span>Phone');
            $(".settings li").eq(3).find(".boxTit").html('<span class="tursIcon"></span>Email');
            $(".settings li").eq(4).find(".boxTit").html('<span class="tursIcon"></span>Country');
            $(".settings li").eq(5).find(".boxTit").html('<span class="tursIcon"></span>Province');
            $(".settings li").eq(6).find(".boxTit").html('<span class="tursIcon"></span>City');
            $(".settings li").eq(7).find(".boxTit").html('<span class="tursIcon"></span>City');
            $(".settings li").eq(8).find(".boxTit").html('<span class="tursIcon"></span>Address');
            $(".settings li").eq(9).find(".boxTit").html('Units');
            $(".settings li").eq(10).find(".boxTit").html('<span class="tursIcon"></span>Post Code');

            $(".addBox input").eq(0).attr("placeholder",'');
            $(".addBox input").eq(1).attr("placeholder",'');
            $(".addBox input").eq(2).attr('placeholder',"");
            $(".addBox input").eq(3).attr('placeholder',"");

            $("#csf span").html('');
            $("#cgj span").html('');
            $("#ccs span").html('');

            $(".addBox input").eq(7).attr('placeholder','');
            $(".addBox input").eq(8).attr('placeholder','');
            $(".addBox input").eq(9).attr('placeholder','');
            $(".addBox input").eq(10).attr('placeholder','');
            $(".addBox input").eq(11).attr('placeholder','');

            $(".loginBtn").html('Save');
        }
    }
</script>
</html>
